<template>
    <div id="UserSerach">
          <div class="wrap">
      <div class="content" >
        <div class="title-head"><h1 class="receipt">“{{$store.state.searchMsg}}”的相关用户</h1></div>
        <ul class="userlist">
            <li v-for="user in userlist" :key="user.userId"><div class="left"><a href="#" class="user-img"><img :src="user.headImg?user.headImg:'https://tx1.douguo.com/upload/photo/d/0/8/140_u69299134989858155737.jpeg'"></a><a href="javascript:void(0);" class="focuson" ><span :id="user.userId" @click="handlefocus($event)">{{user.follow}}</span></a></div>
            <div class="right"><h3><a href="" class="nickname">{{user.nickname}}</a></h3><p class="cate">普通用户</p><p>粉丝  {{user.fans}}   菜谱  {{user.menuNum}}</p></div></li>
        </ul>
      </div>
</div>
<span v-show="displaycontent" class="nocontent">暂时没有更多的内容~</span><div v-show="buttonpage">
     <el-pagination  background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNow"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination></div>
    </div>
</template>
<script>
// import LoginVue from '../../Login.vue'
// import cookie from '../../../cookien/index'

export default {
  inject: ['reload'], // 注入依赖
  data () {
    return {
      userlist: '',
      total: 0,
      userheadImg: '',
      pageNow: 1,
      pageSize: 1,
      searchmessage: '',
      displaycontent: false,
      buttonpage: true
    }
  },
  created () {
    this.Searchusers()
  },
  methods: {
    // 搜索遍历用户
    Searchusers () {
      this.$axios({
        url: '/Menu/Visitor/findUserByNameLike',
        params: {
          pageNow: this.pageNow,
          pageSize: this.pageSize,
          key: this.$store.state.searchMsg
        },
        headers: {
          token: this.$store.state.token
        }
      }).then(data => {
        if (data.data.object.list.length === 0) {
          this.displaycontent = !this.displaycontent
          this.buttonpage = !this.buttonpage
        } else {
          this.userlist = data.data.object.list
          this.total = data.data.object.total
          // for循环获取被关注用户id
          for (let i = 0; i < data.data.object.list.length; i++) {
            const userTwoId = data.data.object.list[i].userId
            this.judgefocus(userTwoId, i)
          }
        }
      })
    },
    // 监测当前一页有几条数据
    handleSizeChange (size) {
      this.pageSize = size
      this.Searchusers()
    },
    // 监测当前页数
    handleCurrentChange (num) {
      this.pageNow = num
      this.Searchusers()
    },

    // 判断是否关注
    judgefocus (userTwoId, e) {
      this.$axios({
        method: 'post',
        url: '/Menu/User/judgeOfAttention',
        params: {
          userOneId: this.$store.state.userMsg.userId,
          userTwoId: userTwoId
        },
        headers: {
          token: this.$store.state.token
        }
      }).then(data => {
        if (data.data.code === 200) {
          if (data.data.message === '已经关注了') {
            this.$set(this.userlist[e], 'follow', '已关注')
          } else {
            this.$set(this.userlist[e], 'follow', '关注')
          }
        }
      })
    },
    // 关注功能
    handlefocus (e) {
      console.log(e)
      this.$axios({
        method: 'post',
        url: '/Menu/User/attention',
        params: {
          userOneId: this.$store.state.userMsg.userId,
          userTwoId: e.target.getAttribute('id')
        },
        headers: {
          token: this.$store.state.token
        }
      }).then(data => {
        console.log(data)
        if (data.data.message === '关注成功') {
          e.target.innerHTML = '已关注'
          // this.reload()
          this.$router.go(0)
        } else {
          e.target.innerHTML = '关注'
          // this.reload()
          this.$router.go(0)
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.focuson{
  display: inline-block;
    width: 80px;
    margin-top: 14px;
    height: 30px;
    line-height: 28px;
    border-radius: 4px;
    border: 1px solid #333;
    color: #333;
    font-size: 12px;
    text-align: center;
}
a{
    text-decoration: none;
    color: #333;
}
.nocontent{
    margin: 40px;
    display: block;
    text-align: center;
    color: #999;
}
.content{
  width: 1000px;
  margin: 0 auto;
  .title-head{
    line-height: 18px;
    margin-top: 20px;
    .receipt{
      font-size: 18px;
      font-weight: bold
    }
  }
}
.userlist{
    list-style: none;
    min-height: 300px;
    width: 100%;
    list-style: none;
    margin-top: 30px;
    li{
        width: 236px;
    height: 110px;
    margin-right: 92px;
    margin-bottom: 40px;
    float: left;
    .left{
        width: 80px;
    font-size: 0;
    margin-right: 10px;
    height: 100%;
    float: left;
    .user-img{
display: inline-block;
    width: 64px;
    height: 64px;
    margin-left: 6px;
    border-radius: 50%;
    overflow: hidden;
    img{
        width: 100%;
        height: 100%;
    }
    }
    .btn{
        display: inline-block;
    width: 80px;
    margin-top: 14px;
    height: 30px;
    line-height: 28px;
    border-radius: 4px;
    border: 1px solid #333;
    color: #333;
    font-size: 12px;
    text-align: center;}
    }
    .right{
        width: 137px;
    margin-right: 9px;
    float: right;
    height: 100%;
    h3{
        font-size: 15px;
    font-weight: bold;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    margin-top: 6px;
     .nickname{
        display: inline-block;
    max-width: 110px;
    vertical-align: middle;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    }
    }
     p{
        margin-top: 24px;
    font-size: 13px;
    line-height: 13px;
    color: #999;
    }
    }
    }
}
.el-pagination{
text-align: center
}
</style>
